<?php

class SYS
{
    static function get_area_jw_info($UserID, $CID)
    {//获取城市区域经纬度；$UserID=用户ID；$CID=城市ID
        global $db, $mc;
        global $SYS_AREA_NUM;
        $strmemo = "";
        if ($CID > 0)
        {
            $strmemo = MakeMemcache::get_memcache_small("V5_AR_JW_INFO_" . $CID);
            if ($strmemo == "")
            {
                $sql = "SELECT * FROM okdkyao.dk_app_area_jw WHERE CID='" . $CID . "' ORDER BY ONum ASC ";
                $res = DB::select_db($UserID, $sql, 1);
                for ($j = 1; $j < $SYS_AREA_NUM; $j++)
                {
                    $areajw = "";
                    if (!empty($res))
                    {
                        $n = 0;
                        for ($i = 0; $i < count($res); $i++)
                        {
                            if ($res[$i]['AID'] == $j)
                            {
                                $areajw[$n]['CID'] = $res[$i]['CID'];
                                $areajw[$n]['JD'] = $res[$i]['JinDuX'];
                                $areajw[$n]['WD'] = $res[$i]['WeiDuY'];
                                $n++;
                            }
                        }
                        $strmemo[$j] = $areajw;
                    }
                }
                MakeMemcache::new_memcache("V5_AR_JW_INFO_" . $CID, $strmemo);
            }
        }
        else
        {
            $strmemo = MakeMemcache::get_memcache_small("V5_AR_JW_INFO_ALL");
            if ($strmemo == "")
            {
                $sql = "SELECT * FROM okdkyao.dk_app_area_jw ORDER BY ONum ASC ";
                $res = DB::select_db($UserID, $sql, 1);
                for ($j = 1; $j < $SYS_AREA_NUM; $j++)
                {
                    $areajw = "";
                    if (!empty($res))
                    {
                        $n = 0;
                        for ($i = 0; $i < count($res); $i++)
                        {
                            if ($res[$i]['AID'] == $j)
                            {
                                $areajw[$n]['CID'] = $res[$i]['CID'];
                                $areajw[$n]['JD'] = $res[$i]['JinDuX'];
                                $areajw[$n]['WD'] = $res[$i]['WeiDuY'];
                                $n++;
                            }
                        }
                        $strmemo[$j] = $areajw;
                    }
                }
                MakeMemcache::new_memcache("V5_AR_JW_INFO_ALL", $strmemo);
            }
        }

        return $strmemo;
    }

    static function is_range_area($lng, $lat)
    {//判断是否在区域范围，如果是，则返回区域ID和城市ID
        global $SYS_AREA_NUM;

        if ($lng < $lat)
        {
            $TmpJD = $lng;
            $lng = $lat;
            $lat = $TmpJD;
        }

        $strmemojw = SYS::get_area_jw_info(1, 0);

        if (empty($strmemojw))
        {
            return false;
        }
        for ($k = 1; $k < $SYS_AREA_NUM; $k++)
        {//查看经纬度在那个区域
            $areajw = $strmemojw[$k];

            if (!empty($areajw))
            {
                $getstate = Util::make_jingwei_area($lng, $lat, $areajw);

                //如果返回 1 , 则表示在区域范围
                if ($getstate == 1)
                {

                    $result['AID'] = $k;
                    $result['CID'] = intval($areajw[0]['CID']);
                    return $result;
                }

            }
        }

        return false;
    }

    static function get_yaodian_info($UserID, $YID, $AID)
    {//读取药店信息
        global $db, $mc;
        $strmemo = "";
        $strmemo = MakeMemcache::get_memcache_small("V5_YD_INFO");
        if (empty($strmemo))
        {
            $sql = "SELECT ID,AID,Caption,ShortCaption,Address,JinDuX,WeiDuY,Memo,Pic1,Pic2,Pic3,Pic4,Pic5,Pic6,Pic7,Pic8,Pic9,Pic10,Pic11,Pic12,Pic13,Pic14,Pic15,Pic16,TY FROM okdkyao.dk_app_yaodian WHERE State=1 AND AID>0";
            $res = DB::select_db($UserID, $sql, 1);
            if (!empty($res))
            {
                for ($i = 0; $i < count($res); $i++)
                {
                    $strmemo[$i]['ID'] = $res[$i]['ID'];
                    $strmemo[$i]['AID'] = $res[$i]['AID'];
                    $strmemo[$i]['TL'] = $res[$i]['Caption'];       //药店名称
                    $strmemo[$i]['STL'] = $res[$i]['ShortCaption']; //简写药店名称
                    $strmemo[$i]['AD'] = $res[$i]['Address'];       //药店地址
                    if ($res[$i]['JinDuX'] > $res[$i]['WeiDuY'])
                    {
                        $strmemo[$i]['JD'] = $res[$i]['JinDuX'];        //经度Y
                        $strmemo[$i]['WD'] = $res[$i]['WeiDuY'];        //纬度X
                    }
                    else
                    {
                        $strmemo[$i]['JD'] = $res[$i]['WeiDuY'];        //经度Y
                        $strmemo[$i]['WD'] = $res[$i]['JinDuX'];        //纬度X
                    }
                    $strmemo[$i]['MM'] = $res[$i]['Memo'];          //药店简介
                    $strmemo[$i]['P1'] = trim($res[$i]['Pic1']);            //药店LOGO
                    $strmemo[$i]['P2'] = trim($res[$i]['Pic2']);            //实景图1
                    $strmemo[$i]['P3'] = trim($res[$i]['Pic3']);            //实景图2
                    $strmemo[$i]['P4'] = trim($res[$i]['Pic4']);            //实景图3
                    $strmemo[$i]['P5'] = trim($res[$i]['Pic5']);            //实景图4
                    $strmemo[$i]['P6'] = trim($res[$i]['Pic6']);            //实景图5
                    $strmemo[$i]['P7'] = trim($res[$i]['Pic7']);            //实景图6
                    $strmemo[$i]['P8'] = trim($res[$i]['Pic8']);            //实景图7
                    $strmemo[$i]['P9'] = trim($res[$i]['Pic9']);            //实景图8
                    $strmemo[$i]['TY'] = intval($res[$i]['TY']);            //药店类型

                    $nn = 10;
                    for ($j = 10; $j < 17; $j++)
                    {
                        if (trim($res[$i]['Pic' . $j]) <> "")
                        {
                            $strmemo[$i]['P' . $nn] = trim($res[$i]['Pic' . $j]);
                            $nn++;
                        }
                    }
                }
                MakeMemcache::new_memcache("V5_YD_INFO", $strmemo);
            }
        }
        $rememo = "";
        if (!empty($strmemo))
        {
            for ($i = 0; $i < count($strmemo); $i++)
            {
                if ($YID > 0 && $YID == $strmemo[$i]['ID'])
                {
                    $rememo = $strmemo[$i];
                    break;
                }
                else if ($AID > 0 && $AID == $strmemo[$i]['AID'])
                {
                    $rememo = $strmemo[$i];
                    break;
                }
            }
        }

        return $rememo;
    }
}
